package com.example.dell1.helloworld;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class data_interact {
    public static String getinfo() throws IOException {
        String r = path + "/getall";
        URL url = new URL(r);
        HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
        urlConnect.setRequestMethod("GET"); //请求方式
        urlConnect.connect();
        int responseCode = urlConnect.getResponseCode();
        if (urlConnect.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = urlConnect.getInputStream();
            BufferedReader buff = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder response=new StringBuilder();
            String line=null;
            while((line=buff.readLine())!=null){
                response.append(line);
            }
            if(urlConnect!=null){
                urlConnect.disconnect();
            }
            return response.toString();
        }
        return null;
    }
    public static void insert(String name, String id) throws IOException{
        String r = path+"/insert"+"?"+"name="+name+"&id="+id;
        //System.out.println(r);
        URL url = new URL(r);
        HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
        urlConnect.setRequestMethod("GET"); //请求方式
        urlConnect.connect();
        int responseCode = urlConnect.getResponseCode();
        if(urlConnect!=null){
            urlConnect.disconnect();
        }
    }
    public static void delete(String name, String id)throws IOException{
        String r = path+"/delete"+"?"+"name="+name+"&id="+id;
        URL url = new URL(r);
        HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
        urlConnect.setRequestMethod("GET"); //请求方式
        urlConnect.connect();
        int responseCode = urlConnect.getResponseCode();
        if(urlConnect!=null){
            urlConnect.disconnect();
        }
    }
    public static void update(String name, String id)throws IOException{
        String r = path+"/update"+"?"+"name="+name+"&id="+id;
        URL url = new URL(r);
        HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
        urlConnect.setRequestMethod("GET"); //请求方式
        urlConnect.connect();
        int responseCode = urlConnect.getResponseCode();
        if(urlConnect!=null){
            urlConnect.disconnect();
        }
    }
    private static String path = "http://10.0.2.2:8080/db";

}
